package com.lovo.project.service.impl;
import com.lovo.project.dao.EmployDAO;
import com.lovo.project.enums.EmployEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.EmployeeDO;
import com.lovo.project.service.EmployService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

/**
 * @author 955
 * @title 店员业务实现类
 * @date 2020/11/3 20:26
 */
public class EmployServiceImpl implements EmployService {
    @Override
    public void insertEmploy(EmployeeDO ed) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        EmployDAO dao=sqlSession.getMapper(EmployDAO.class);
        try{
            int isInsert=dao.insertEmploy(ed);
            if(isInsert<=0){
                throw new OurException(EmployEnum.EMPLOY_INSERTEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void deleteEmploy(Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        EmployDAO dao=sqlSession.getMapper(EmployDAO.class);
        try{
            int isDel=dao.deleteEmploy(id);
            if(isDel <=0){
                throw new OurException(EmployEnum.EMPLOY_DELETEEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public EmployeeDO selectById(Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        EmployDAO dao=sqlSession.getMapper(EmployDAO.class);
        try{
            EmployeeDO ed=dao.selectById(id);
            if(ed==null){
                throw new OurException(EmployEnum.EMPLOY_FINDEXP_ENUM);
            }
            return ed;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public List<EmployeeDO> selectAll(String type, String value) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        EmployDAO dao=sqlSession.getMapper(EmployDAO.class);
        try{
            List<EmployeeDO>list=dao.selectAll(type,value);
            if(list==null){
                throw new OurException(EmployEnum.EMPLOY_FINDEXP_ENUM);
            }
            return list;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void update(EmployeeDO ed) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        EmployDAO dao=sqlSession.getMapper(EmployDAO.class);
        try{
            int isUpdate=dao.update(ed);
            if(isUpdate<=0){
                throw new OurException(EmployEnum.EMPLOY_UPDATEEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public List<EmployeeDO> byShopIdSelectEmployee(String type, String value, Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        EmployDAO dao=sqlSession.getMapper(EmployDAO.class);
        try{
            List<EmployeeDO>list=dao.byShopIdSelectEmployee(type,value,id);
            if(list==null){
                throw new OurException(EmployEnum.EMPLOY_FINDEXP_ENUM);
            }
            return list;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }
    }
}
